﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace M1Xonix
{
    /// <summary>
    /// See klass joonistab punktide akna ja peab punktiarvestust
    /// </summary>
    internal class ScoreBox
    {
        private readonly SpriteFont _font;
        private readonly int _height;
        private readonly int _scoreBoxHalfWidth;
        private readonly Texture2D _texture;
        private readonly int _width;
        private readonly int _x;
        private readonly int _y;
        private int _level = 1;
        private float _levelX, _levelY;
        private int _livesLeft = 3;
        private float _livesX, _livesY;
        private int _score;
        private float _scoreX, _scoreY;

        /// <summary>
        /// ScoreBoard konstruktor
        /// </summary>
        /// <param name="texture">tausta tekstuur</param>
        /// <param name="x">asukoht x</param>
        /// <param name="y">asukoht y</param>
        /// <param name="width">laius</param>
        /// <param name="height">kõrgus</param>
        /// <param name="font">teksti font</param>
        public ScoreBox(Texture2D texture, int x, int y, int width, int height, SpriteFont font)
        {
            _x = x;
            _y = y;
            _width = width;
            _height = height;
            _texture = texture;
            _font = font;
            _scoreBoxHalfWidth = _width/2;
            CalculateCoordinates();
        }

        private void CalculateCoordinates()
        {
            _scoreX = _x + 50;
            _scoreY = _y + 5;
            _levelX = _scoreBoxHalfWidth - 30;
            _levelY = _y + 5;
            _livesX = _width - 140;
            _livesY = _y + 5;
        }

        /// <summary>
        /// Liidab skoorile otsa
        /// </summary>
        /// <param name="addScore"></param>
        public void AddScore(int addScore)
        {
            _score += addScore;
        }

        /// <summary>
        /// tõstab taset/levelit
        /// </summary>
        public void AddLevel()
        {
            _level++;
        }

        /// <summary>
        /// tapab ühe elu
        /// </summary>
        public void KillPlayer()
        {
            _livesLeft--;
        }

        /// <summary>
        /// tagastab true, kui elud on otsas
        /// </summary>
        /// <returns></returns>
        public bool AreLivesLeft()
        {
            return _livesLeft < 0;
        }

        /// <summary>
        /// Joonistab skoorikasti
        /// </summary>
        /// <param name="spriteBatch">SpriteBatch</param>
        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(_texture, new Rectangle(_x, _y, _width, _height), Color.White);
            spriteBatch.DrawString(_font, "Score: " + _score, new Vector2(_scoreX, _scoreY), Color.Yellow);
            spriteBatch.DrawString(_font, "Level: " + _level, new Vector2(_levelX, _levelY), Color.Yellow);
            string livesLeftText = _livesLeft < 0 ? "Player dead" : ("Lives left: " + _livesLeft.ToString());
            spriteBatch.DrawString(_font, livesLeftText, new Vector2(_livesX, _livesY), Color.Yellow);
        }

        public int Score
        {
            get { return _score; }
        }
    }


}